package com.kaka.bus.engine.borrow.bank;

import android.app.Activity;

import com.kaka.bus.bean.BankBean;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.NamePhoneBean;
import com.kaka.bus.bean.Single;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.engine.RouterUtil;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;
import com.kaka.bus.util.U;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2016/10/19.
 * e-mail : ${email}
 * desc :
 */

public class BankCardPresenter extends RxPresenter<BackCardContract.View> implements BackCardContract.Presenter {

    private String mCode;
    private String mUserPhone;

    @Inject
    public BankCardPresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public boolean sendyzm(String phone,String bankcard ,String tokenKey) {

        mView.showDialog("发送验证码...");

        addSubscribe(mDataManager
                .getApi()
                .getPayCode(phone,tokenKey,"sendDynamic", Constant.PLATFORM,bankcard)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<Single>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<Single> bean) {
                       mView.sendSmsSuccess(bean);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));
        return true;
    }

    @Override
    public void getBankList() {
        addSubscribe(mDataManager
                .getApi()
                .getBankList()
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<List<BankBean>>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<List<BankBean>> data) {
                        mView.getBankListSuccess(data);
                    }
                }));
    }

    @Override
    public void sendService(Map<String, String> map, String tokenKey,String token) {
            mView.showDialog("提交银行卡信息");
            addSubscribe(mDataManager
                    .getApi()
                    .commitBankInfo(map,tokenKey,token,Constant.PLATFORM)
                    .compose(T.D())
                    .subscribeWith(new Result<HttpWrapper<String>>() {
                        @Override
                        protected void onSuccess(HttpWrapper<String> bean) {
                            mView.sendServiceSuccess(bean);
                        }

                        @Override
                        protected void onFinish() {
                            mView.dissDialog();
                        }
                    }));

    }

    @Override
    public void readInfo(String userId,String token) {
        addSubscribe(mDataManager
                .getApi()
                .getNamePhone(userId,token,U.Sign(userId,token))
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<NamePhoneBean>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<NamePhoneBean> bean) {

                        mView.getInfoSuccess(bean);

                    }
                }));
    }

    @Override
    public void router(Activity activity) {
        mView.showDialog("查询节点中...");
        RouterUtil.router(activity, mDataManager.getApi(),mCompositeDisposable, () -> {
            if(mView != null){
                mView.dissDialog();
            }
        });
    }
}
